<?php
class ProcessesController extends AppController {

    var $name = 'Processes';
    var $helpers = array('Ajax', 'Javascript', 'Paginator', 'Text');
    var $components = array('RequestHandler');

    function admin_index($clientId = null) {
        $this->Process->recursive = 0;
        if ($clientId != null) {
            $processes = $this->Process->find('list', array(
                'conditions' => array('Process.client_id =' => $clientId)
                )
            );
        }
        else {
            $processes = $this->paginate();
        }

        $this->set('processes', $processes);
    }

    function admin_view($id = null) {
        if (!$id) {
            $message = sprintf(__('I18N_INVALID_ENTITY'),__('I18N_PROCESS'));
            $this->showMessage($message);
        }
        $this->set('process', $this->Process->read(null, $id));
    }

    function admin_add($clientId = 0,$workflowId = 0) {
        if (!empty($this->data)) {
            $this->Process->create();
            if ($this->Process->save($this->data)) {
                $message = __('I18N_SUCCESSFULY_ADDED');
            } else {
                $message = __('I18N_ERROR_NOT_ADDED');
            }
            $this->showMessage($message);
        }
        $clients = $this->Process->Client->find('list');
        $workflows = $this->Process->Workflow->find('list');
        $this->set('clients',compact('clients'));
        $this->set('workflows',compact('workflows'));
        $this->set('preSelectedClientId', $clientId);
        $this->set('preSelectedWorkflowId', $workflowId);
    }

    function admin_edit($id = null) {
        if (!$id && empty($this->data)) {
            $message = sprintf(__('I18N_INVALID_ENTITY'),__('I18N_PROCESS'));
            $this->showMessage($message);
        }
        if (!empty($this->data)) {
            if ($this->Process->save($this->data)) {
                $message = 'I18N_SUCCESSFULY_UPDATED';
            } else {
                $message = 'I18N_ERROR_NOT_UPDATED';
            }
            $this->showMessage($message);
        }
        if (empty($this->data)) {
            $this->data = $this->Process->read(null, $id);
        }
        $clients = $this->Process->Client->find('list');
        $workflows = $this->Process->Workflow->find('list');
        $this->set('clients',compact('clients'));
        $this->set('workflows',compact('workflows'));
    }

    function admin_delete($id = null) {
        if (!$id) {
            $message = sprintf(__('I18N_INVALID_ENTITY'),__('I18N_PROCESS'));
            $this->showMessage($message);
        }
        if ($this->Process->delete($id)) {
            $message = 'I18N_SUCCESSFULY_REMOVED';
        }
        else {
            $message = 'I18N_ERROR_NOT_REMOVED';
        }
        $this->showMessage($message);
    }
}
?>